﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.gexin.rp.sdk.dto;
using com.igetui.api.openservice;
using com.igetui.api.openservice.igetui;
using com.igetui.api.openservice.igetui.template;
using com.igetui.api.openservice.payload;
using System.Net;

/**
 * 
 * 说明：
 *      此工程是一个测试工程，所用的相关.dll文件，都已经存在protobuffer文件里，需要加载到References里。
 *      工程中还有用到一个System.Web.Extensions文件，这个文件是用到Framework里V4.0版本的，
 *      一般路径如下：C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0，
 *      或如下路径：C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5没有的也可以在protobuffer
 *      文件夹里找到。如再有问题，请直接联系技术客服，谢谢！
 *      GetuiServerApiSDK：此.dll文件为个推C#版本的SDK文件
 *      Google.ProtocolBuffers：此.dll文件为Google的数据交换格式文件
 *  注：
 *      新增一个连接超时时间设置，通过在环境变量--用户变量中增加名为：GETUI_TIMEOUT 的变量（修改环境变量，
       电脑重启后才能生效），值则是超时时间，如不设定，则默认为20秒。
 **/

namespace MessagePush
{
    public class PushMessage
    {
        //参数设置 <-----参数需要重新设置----->
        //http的域名
        private static String HOST = "http://sdk.open.api.igexin.com/apiex.htm";
        //https的域名
        //private static String HOST = "https://api.getui.com/apiex.htm";

        //定义常量, appId、appKey、masterSecret 采用本文档 "第二步 获取访问凭证 "中获得的应用配置

        //test
        //private static String APPID = "l1xbah0obY7mHfqGURV7p4";
        //private static String APPKEY = "vn5JCiSrL3AGMZaJbcjUc8";
        //private static String MASTERSECRET = "nXhabTozI7AfYVmmMVpVX8";
        //正式
        private static String APPID = "c4LbR60qfr8T9y4cqWR6A7";
        private static String APPKEY = "KWLN6Jc5Eb9Akz0nXeA7J1";
        private static String MASTERSECRET = "QVtuE4sqMo8JbVGsucrNp7";

        //private static String CLIENTID = "27f61adb1b720431620522411477c3b7";//
        // private static String CLIENTID = "a1809a8c18c86fbe7f9834285e0ee213";
        private static String DeviceToken = "";  //填写IOS系统的DeviceToken
        //别名推送方式
        //private static String ALIAS = "";
        //static void Main(string[] args)
        //{
        //    //toList接口每个用户状态返回是否开启，可选
        //    Console.OutputEncoding = Encoding.GetEncoding(936);
        //    Environment.SetEnvironmentVariable("needDetails", "true");

        //    string title = "新消息";
        //    string tzContent = "我是大龙哥哥";
        //    string content = "{\"add\":\"123\"}";
        //    string logo = "";
        //    string wllogo = "";
        //    //NotificationTemplate template = NotificationTemplateDemo(title, tzContent, content, logo, wllogo);
        //    string type = "2";
        //    PushMessageToSingle(CLIENTID, title, tzContent, content, logo, wllogo, type);
        //}

        /// <summary>
        /// 推送消息
        /// </summary>
        /// <returns></returns>
        public static string PushMessageToSingle(string cid, string title, string tzContent, string content, string logo, string wllogo, string type)
        {
            string pushResult = "";
            if (type == "1")
            { //android
              //NotificationTemplate templateAndroid = TransmissionTemplateAndroid(title, tzContent, content, logo, wllogo);
              // //TransmissionTemplate templateAndroid = TransmissionTemplateDemo();
              // message.Data = templateAndroid;
                pushResult= UmMsg.SendMsg(title, tzContent, content, cid);
            }
            else
            {
               
                IGtPush push = new IGtPush(HOST, APPKEY, MASTERSECRET);
                // 单推消息模型
                SingleMessage message = new SingleMessage();
                message.IsOffline = true;                         // 用户当前不在线时，是否离线存储,可选
                message.OfflineExpireTime = 1000 * 3600 * 12;            // 离线有效时间，单位为毫秒，可选
                                                                         //调用模板
                if (type == "2")//ios
                {
                    TransmissionTemplate templateIOS = TransmissionTemplateIOS(title, tzContent, content);
                    message.Data = templateIOS;
                }
                //判断是否客户端是否wifi环境下推送，2为4G/3G/2G，1为在WIFI环境下，0为不限制环境
                message.PushNetWorkType = 0;
                com.igetui.api.openservice.igetui.Target target = new com.igetui.api.openservice.igetui.Target();
                target.appId = APPID;
                target.clientId = cid;
                //target.alias = ALIAS;
                try
                {
                    pushResult = push.pushMessageToSingle(message, target);
                }
                catch (RequestException e)
                {
                    String requestId = e.RequestId;
                    //发送失败后的重发
                    pushResult = push.pushMessageToSingle(message, target, requestId);
                }
            }
            return pushResult;
        }
        //通知透传模板动作内容
        public static NotificationTemplate TransmissionTemplateAndroid(string title, string tzContent, string content, string logo, string wllogo)
        {
            NotificationTemplate template = new NotificationTemplate();
            template.AppId = APPID;
            template.AppKey = APPKEY;
            //通知栏标题
            template.Title = title;

            byte[] bpath = System.Text.Encoding.UTF8.GetBytes(tzContent);
            tzContent = System.Text.ASCIIEncoding.UTF8.GetString(bpath);
            //通知栏内容     
            template.Text = tzContent;
            //通知栏显示本地图片
            template.Logo = logo;
            //通知栏显示网络图标
            template.LogoURL = wllogo;
            //应用启动类型，1：强制应用启动  2：等待应用启动
            template.TransmissionType = "2";
            //透传内容  
            template.TransmissionContent = content;
            //接收到消息是否响铃，true：响铃 false：不响铃   
            template.IsRing = true;
            //接收到消息是否震动，true：震动 false：不震动   
            template.IsVibrate = true;
            //接收到消息是否可清除，true：可清除 false：不可清除    
            template.IsClearable = true;
            ////设置通知定时展示时间，结束时间与开始时间相差需大于6分钟，消息推送后，客户端将在指定时间差内展示消息（误差6分钟）
            //String begin = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            //String end = DateTime.Now.AddMinutes(7).ToString("yyyy-MM-dd HH:mm:ss");
            //template.setDuration(begin, end);

            return template;
        }
        public static TransmissionTemplate TransmissionTemplateDemo()
        {
            TransmissionTemplate template = new TransmissionTemplate();
            template.AppId = APPID;
            template.AppKey = APPKEY;
            //应用启动类型，1：强制应用启动 2：等待应用启动
            template.TransmissionType = "2";
            //透传内容  
            template.TransmissionContent = "透传内容";

            return template;
        }
        //ios
        public static TransmissionTemplate TransmissionTemplateIOS(string title, string tzContent, string content)
        {
            TransmissionTemplate template = new TransmissionTemplate();
            template.AppId = APPID;
            template.AppKey = APPKEY;
            template.TransmissionType = "2";            //应用启动类型，1：强制应用启动 2：等待应用启动
            template.TransmissionContent = content;  //透传内容

            //iOS简单推送
            //APNPayload apnpayload = new APNPayload();
            //SimpleAlertMsg alertMsg = new SimpleAlertMsg(tzContent);
            //apnpayload.AlertMsg = alertMsg;
            //apnpayload.Badge = 11;
            //apnpayload.ContentAvailable = 1;
            //apnpayload.Category = "";
            //apnpayload.Sound = "";
            //apnpayload.addCustomMsg("apnpayload", content);
            //template.setAPNInfo(apnpayload);
            byte[] bpath = System.Text.Encoding.UTF8.GetBytes(tzContent);
            tzContent = System.Text.ASCIIEncoding.UTF8.GetString(bpath);
            //APN高级推送
            APNPayload apnpayload = new APNPayload();
            DictionaryAlertMsg alertMsg = new DictionaryAlertMsg();
            alertMsg.Body = tzContent;
            alertMsg.ActionLocKey = "ActionLocKey";
            alertMsg.LocKey = "图片";
            alertMsg.addLocArg("LocArg");
            alertMsg.LaunchImage = "LaunchImage";
            //iOS8.2支持字段
            alertMsg.Title = title;
            alertMsg.TitleLocKey = "TitleLocKey";
            alertMsg.addTitleLocArg("TitleLocArg");

            apnpayload.AlertMsg = alertMsg;
            apnpayload.Badge = 1;
            apnpayload.ContentAvailable = 1;
            //apnpayload.Category = "";
            apnpayload.Sound = "test1.wav";
            apnpayload.addCustomMsg("payload", "payload");
            template.setAPNInfo(apnpayload);

            //设置客户端展示时间
            // String begin = "2015-03-06 14:28:10";
            //String end = "2015-03-06 14:38:20";
            // template.setDuration(begin, end);

            return template;
        }
        //暂未用
        public static NotyPopLoadTemplate NotyPopLoadTemplateDemo()
        {
            NotyPopLoadTemplate template = new NotyPopLoadTemplate();
            template.AppId = APPID;
            template.AppKey = APPKEY;
            //通知栏标题
            template.NotyTitle = "请填写通知标题";
            //通知栏内容
            template.NotyContent = "请填写通知内容";
            //通知栏显示本地图片
            template.NotyIcon = "icon.png";
            //通知栏显示网络图标
            template.LogoURL = "http://www-igexin.qiniudn.com/wp-content/uploads/2013/08/logo_getui1.png";
            //弹框显示标题
            template.PopTitle = "弹框标题";
            //弹框显示内容    
            template.PopContent = "弹框内容";
            //弹框显示图片    
            template.PopImage = "";
            //弹框左边按钮显示文本    
            template.PopButton1 = "下载";
            //弹框右边按钮显示文本    
            template.PopButton2 = "取消";
            //通知栏显示下载标题
            template.LoadTitle = "下载标题";
            //通知栏显示下载图标,可为空 
            template.LoadIcon = "file://push.png";
            //下载地址，不可为空
            template.LoadUrl = "http://www.appchina.com/market/d/425201/cop.baidu_0/com.gexin.im.apk";
            //应用安装完成后，是否自动启动
            template.IsActived = true;
            //下载应用完成后，是否弹出安装界面，true：弹出安装界面，false：手动点击弹出安装界面 
            template.IsAutoInstall = true;
            //接收到消息是否响铃，true：响铃 false：不响铃
            template.IsBelled = true;
            //接收到消息是否震动，true：震动 false：不震动   
            template.IsVibrationed = true;
            //接收到消息是否可清除，true：可清除 false：不可清除    
            template.IsCleared = true;
            return template;
        }



    }
}

